package com.ubi.core.framework.job;

import com.ubi.core.utils.StopWatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author raymond
 */
public abstract class Job implements Runnable {

    private final Logger logger = LoggerFactory.getLogger(Job.class);

    @Override
    public void run() {
        try {
            logger.debug("begin execute job={}", this.getClass().getName());
            StopWatch stopWatch = new StopWatch();

            before();
            execute();
            after();

            logger.debug("finish execute job={},elapsedTime={}", this.getClass().getName(), stopWatch.elapsedTime());
        } catch (Exception e) {
            logger.error("error occur when execute job={}", this.getClass().getName());
            logger.error("exception trace", e);
        }
    }

    /**
     * you can overwrite this method to do exception handler
     *
     * @param e
     */
    public void exceptionHandler(Exception e) {
    }

    public abstract void execute() throws Exception;

    public void before() {
    }

    public void after() {
    }

}
